<template>
  <div>
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
    />
    <van-popup v-model="showEdit" position="bottom">
      <van-address-edit
        :area-list="areaList"
        show-postal
        show-delete
        show-set-default
        show-search-result
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        :address-info="list[2]"
        @save="onSave"
        @delete="showEdit = false"
      />
    </van-popup>
  </div>
</template>

<script>
// import areaList from '@/config/area.js'
import {getaddress} from '@/request/api.js'
export default {
  data() {
    return {
      chosenAddressId: "1",
      areaList:[],
      showEdit:false,
      list: [
        {
          id: "3",
          name: "雷佳豪",
          tel: "13100000000",
          province: "广东省",
          city: "天河市",
          addressDetail: "叩丁狼",
          areaCode: "110101",
          postalCode: "520555",
          isDefault: true
        }
      ]
    };
  },
  methods: {
    onAdd() {
      this.$toast("新增地址");
      // this.$router.push("/user/address/edit");
      this.showEdit = true
    },
    onEdit(item, index) {
      console.log(item);
      this.$toast("编辑地址:" + index);
      this.showEdit = true
    },
    onSave(val){
      console.log(val)
    }
  },
  created() {
    // getaddress({
    //   headers:{"X-Nideshop-Token":localStorage.getItem("token")},
    //   params:{
    //     parentId:110101,
    //   }
    // }).then(res => {
    //   console.log(res)
    // })

    let addressList = JSON.parse(localStorage.getItem("addressList"));
    if (addressList && addressList.length > 0) {
      let myAddress = [];
      addressList.forEach(element => {
        if (localStorage.getItem("token") == element.token)
          return (myAddress = addressList.myAddress);
      });
      if (myAddress.length > 0) {
        this.list = myAddress.reduce((prev, current) => {
          let { id, name, tel } = current;
          let obj = {
            id,
            name,
            tel
          };
          if (current.province == current.city) {
            obj.address = current.province + current.addressDetail;
          } else {
            obj.address =
              current.province + current.city + current.addressDetail;
          }
          prev.push(obj);
          return prev;
        }, []);
      }
    }
    let myDefault = this.list.filter(el => {
      return el.isDefault;
    });
    this.chosenAddressId = myDefault[0].id;

  }
};
</script>

<style>
</style>